package com.example.mymenu;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mymenu.activity.MenuActivity;

import java.util.List;
import java.util.Map;

class HomeAdapter extends RecyclerView.Adapter<HomeAdapter.HomeHolder>{

    private Context context;
    private Map<String,List<String>> titileinfo;
    private String[] titles;
    private Intent intent;
    public HomeAdapter(Context context, Map<String,List<String>> titleinfo, String[] titles) {
        this.context = context;
        this.titileinfo = titleinfo;
        this.titles = titles;
    }

    @NonNull
    @Override
    //获取itemview的视图对象，并将视图对象传递给viewholder
    public HomeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        //View v1 = View.inflate(context,R.layout.itemlayout,null);
        View v1 = LayoutInflater.from(context).inflate(R.layout.itemlayout,parent,false);
        HomeHolder homeHolder = new HomeHolder(v1);
        return homeHolder;
    }

    @Override//将值绑定给holder对象
    public void onBindViewHolder(@NonNull HomeHolder holder, int position) {
        String title = titles[position];
        holder.title.setText(title);
        List<String> list = titileinfo.get(title);
        holder.btnFirst.setText(list.get(0));
        holder.btnSecond.setText(list.get(1));
        holder.btnFirst.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(context, MenuActivity.class);
                intent.putExtra("param",list.get(0));
                context.startActivity(intent);

                Log.d("param",list.get(0));
            }
        });
        holder.btnSecond.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                intent = new Intent(context, MenuActivity.class);
                intent.putExtra("param",list.get(1));
                context.startActivity(intent);

                Log.d("param",list.get(1));

            }
        });
    }

    @Override
    public int getItemCount() {
        return titles.length;
    }


    public class HomeHolder extends RecyclerView.ViewHolder{

        //定义需要赋值控件控件对象
        TextView title;
        Button btnFirst,btnSecond;
        public HomeHolder(@NonNull View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            btnFirst = itemView.findViewById(R.id.btn_first);
            btnSecond = itemView.findViewById(R.id.btn_second);
        }
    }
}
